package cc.rengu.igas.momp.common.dao.impl;

import cc.rengu.igas.momp.common.constant.CommonConstant;
import cc.rengu.igas.momp.common.dao.CouponInfoMapper;
import cc.rengu.igas.momp.common.entity.*;
import cc.rengu.jradp.mods.dbs.Database;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 卡券数据库操作接口实现
 *
 * @author Jinan Liu
 * @version 1.0.0
 * @date 2020/4/7 14:19
 */
public class CouponInfoMapperImpl implements CouponInfoMapper {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.MOMP_POOL_NAME);

    /**
     * 使用营销活动规则查询一张可领取卡券数据库操作接口实现
     *
     * @param tmCouponInfo 卡券信息表
     * @return cc.rengu.igas.momp.common.entity.TMCouponInfo 卡券信息表
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/7 14:19
     */
    @Override
    public TMCouponInfo selectClaimableCouponbyRuleIndex(TMCouponInfo tmCouponInfo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectClaimableCouponbyRuleIndex");

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmCouponInfo, tmCouponInfo);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("法人机构号<{}>获取卡券编号<{}>信息失败! RETURN_CODE=<{}>", tmCouponInfo.getInstId(), tmCouponInfo.getCouponNo(), iReturnCode);
            return null;
        }

        return tmCouponInfo;
    }

    /**
     * 使用卡券编号查询已锁定卡券信息数据库操作接口实现
     *
     * @param tmCouponInfo 卡券信息表
     * @return cc.rengu.igas.momp.common.entity.TMCouponInfo 卡券信息表
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/7 14:17
     */
    @Override
    public TMCouponInfo selectLockedCouponByPrimaryKey(TMCouponInfo tmCouponInfo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectLockedCouponByPrimaryKey");

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmCouponInfo, tmCouponInfo);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("法人机构号<{}>获取卡券编号<{}>信息失败! RETURN_CODE=<{}>", tmCouponInfo.getInstId(), tmCouponInfo.getCouponNo(), iReturnCode);
            return null;
        }

        return tmCouponInfo;
    }

    /**
     * 使用主键更新卡券状态数据库操作接口实现
     *
     * @param tmCouponInfo 卡券信息表
     * @return int 返回码
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/7 14:56
     */
    @Override
    public int updateCouponStatusByPrimaryKey(TMCouponInfo tmCouponInfo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateCouponStatusByPrimaryKey");

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmCouponInfo, TMCouponInfo.class);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("法人机构号<{}>获取卡券编号<{}>信息失败! RETURN_CODE=<{}>", tmCouponInfo.getInstId(), tmCouponInfo.getCouponNo(), iReturnCode);
            return CommonConstant.PROCESS_FAILED;
        }

        return CommonConstant.PROCESS_SUCCESS;
    }

    /**
     * 更新已锁定卡券相关信息数据库操作接口实现
     *
     * @param tmCouponInfo 卡券信息
     * @return int 返回码
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/7 14:55
     */
    @Override
    public int updateLockedCouponInfo(TMCouponInfo tmCouponInfo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateLockedCouponInfo");

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmCouponInfo, TMCouponInfo.class);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("更新法人机构号<{}>卡券编号<{}>信息失败! RETURN_CODE=<{}>", tmCouponInfo.getInstId(), tmCouponInfo.getCouponNo(), iReturnCode);
            return CommonConstant.PROCESS_FAILED;
        }

        return CommonConstant.PROCESS_SUCCESS;
    }

    /**
     * 更新已锁定卡券相关信息和卡券有效期数据库操作接口实现
     *
     * @param tmCouponInfo 卡券信息
     * @return int 返回码
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/7 14:55
     */
    @Override
    public int updateLockedCouponInfoAndDate(TMCouponInfo tmCouponInfo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateLockedCouponInfoAndDate");

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmCouponInfo, TMCouponInfo.class);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("更新法人机构号<{}>卡券编号<{}>信息失败! RETURN_CODE=<{}>", tmCouponInfo.getInstId(), tmCouponInfo.getCouponNo(), iReturnCode);
            return CommonConstant.PROCESS_FAILED;
        }

        return CommonConstant.PROCESS_SUCCESS;
    }

    /**
     * 使用营销活动编号查询指定营销活动对应的所有卡券信息数据库操作接口实现
     *
     * @param cpMcListInfoInObj 卡券类营销活动列表查询使用实体类入参
     * @param pageInfo          数据库操作分页实体类
     * @return cc.rengu.oltp.utility.bean.PageResult 查询结果列表
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/6/1 22:39
     */
    @Override
    public PageResult selectAllCouponInfoByMcNo(CpMcListInfoInObj cpMcListInfoInObj, PageInfo pageInfo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectAllCouponInfoByMcNo");

        return dbsUtil.dbsFetchPageListData(sqlStr, cpMcListInfoInObj, TMCouponInfo.class, pageInfo);
    }

    /**
     * 用户查询卡券列表信息(非分页)
     *
     * @return java.util.List<cc.rengu.igas.momp.common.entity.TMCouponInfo> 卡券信息表
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2021/1/18 16:31
     */
    @Override
    public List<TMCouponInfo> getCouponListInfoByUser(TMCouponInfo tMCouponInfo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "mompSelectCouponInfoByUser");

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, tMCouponInfo, TMCouponInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("用户<{}> 查询个人卡券列表信息失败!", tMCouponInfo.getClaimUserId());
            return null;
        }

        return objectList.stream().map(item -> (TMCouponInfo) (item)).collect(Collectors.toList());
    }

    /**
     * 使用用户ID和营销活动编号查询指定营销活动对应的所有卡券信息数据库操作接口实现(分页)
     *
     * @param cpMcListInfoInObj 卡券类营销活动列表查询使用实体类入参
     * @param pageInfo          数据库操作分页实体类
     * @return cc.rengu.oltp.utility.bean.PageResult 查询结果列表
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/7 15:37
     */
    @Override
    public PageResult getAllCouponDetailInfoByUser(CpMcListInfoInObj cpMcListInfoInObj, PageInfo pageInfo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "getAllCouponDetailInfoByUser");

        return dbsUtil.dbsFetchPageListData(sqlStr, cpMcListInfoInObj, TMCouponInfo.class, pageInfo);
    }

    /**
     * 使用营销活动编号查询指定营销活动对应的所有卡券信息数据库操作接口实现
     *
     * @param instId 法人机构号
     * @param mcNo   营销活动编号
     * @param userId 用户ID
     * @return java.util.List<cc.rengu.igas.momp.common.entity.TMCouponInfo> 卡券信息表
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/7 15:46
     */
    @Override
    public List<TMCouponInfo> selectAllCouponInfoByMcNo(String instId, String mcNo, String userId) throws Exception {
        return null;
    }

    /**
     * 退回卡券时更新卡券信息至初始状态数据库操作接口实现
     *
     * @param couponInfo 卡券信息表
     * @return int 返回码
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/7 20:31
     */
    @Override
    public int updateCouponToInitialStatusByPrimaryKey(TMCouponInfo couponInfo) throws Exception {
        return 0;
    }

    /**
     * 使用卡券编号查询单张卡券信息数据库操作接口实现
     *
     * @param tmCouponInfo 卡券信息表
     * @return cc.rengu.igas.momp.common.entity.TMCouponInfo 卡券信息表
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/17 17:01
     */
    @Override
    public TMCouponInfo selectSingleCouponInfoByCouponNo(TMCouponInfo tmCouponInfo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectSingleCouponInfoByCouponNo");

        String instId = tmCouponInfo.getInstId();
        String couponNo = tmCouponInfo.getCouponNo();

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmCouponInfo, tmCouponInfo);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("法人机构号<{}>获取卡券编号<{}>信息失败! RETURN_CODE=<{}>", instId, couponNo, iReturnCode);
            return null;
        }

        return tmCouponInfo;
    }

    /**
     * 卡券核销冲正数据库操作接口定义
     *
     * @param tmCouponInfo 卡券信息表
     * @return int 返回码
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/23 19:24
     */
    @Override
    public int reverseRedeemedCoupon(TMCouponInfo tmCouponInfo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "reverseRedeemedCoupon");

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmCouponInfo, TMCouponInfo.class);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("法人机构号<{}>冲正卡券编号<{}>核销信息失败! RETURN_CODE=<{}>", tmCouponInfo.getInstId(), tmCouponInfo.getCouponNo(), iReturnCode);
            return CommonConstant.PROCESS_FAILED;
        }

        return CommonConstant.PROCESS_SUCCESS;
    }

    /**
     * 查询用户已领取未核销卡券信息数据库操作接口实现
     *
     * @param instId 法人机构号
     * @param userId 用户ID
     * @return java.util.List<cc.rengu.igas.momp.common.entity.TMCouponInfo> 卡券信息表
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/27 22:03
     */
    @Override
    public List<TMCouponInfo> selectUserCouponBoundListInfo(String instId, String userId, String txnDate) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectUserCouponBoundListInfo");

        // 按接口赋查询条件
        TMCouponInfo tmCouponInfo = new TMCouponInfo();
        tmCouponInfo.setInstId(instId);
        tmCouponInfo.setClaimUserId(userId);
        tmCouponInfo.setCouponStartDate(txnDate);
        tmCouponInfo.setCouponEndDate(txnDate);

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, tmCouponInfo, TMCouponInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("查询机构<{}>的用户<{}>的已领取未核销的卡券信息列表失败!", instId, userId);
            return null;
        }

        return objectList.stream().map(item -> (TMCouponInfo) (item)).collect(Collectors.toList());
    }

    /**
     * 使用卡券基本信息查询可领取卡券信息数据库操作接口实现
     *
     * @param instId 法人机构号
     * @param mcNo   营销活动编号
     * @return cc.rengu.igas.momp.common.entity.TMCouponInfo 卡券信息表
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/6/8 16:52
     */
    @Override
    public TMCouponInfo selectSingleClaimableCouponbyMcNo(String instId, String mcNo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectClaimableCouponbyMcNo");

        TMCouponInfo tmCouponInfo = new TMCouponInfo();
        tmCouponInfo.setInstId(instId);
        tmCouponInfo.setMcNo(mcNo);

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmCouponInfo, tmCouponInfo);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("法人机构号<{}>获取卡券卡券营销活动<{}>可领取卡券信息失败! RETURN_CODE=<{}>", instId, mcNo, iReturnCode);
            return null;
        }

        return tmCouponInfo;
    }

    /**
     * 根据营销活动编号及规则顺序号查询卡券数据库操作接口实现
     *
     * @return java.util.List<cc.rengu.igas.momp.common.entity.TMCouponInfo> 卡券信息表
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/6/20
     */
    @Override
    public List<TMCouponInfo> selectCouponInfoListByRuleIndex(String instId, String mcNo, String ruleIndex) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectCouponInfoListByRuleIndex");

        // 按接口赋查询条件
        TMCouponInfo tmCouponInfo = new TMCouponInfo();
        tmCouponInfo.setInstId(instId);
        tmCouponInfo.setMcNo(mcNo);
        tmCouponInfo.setRuleIndex(ruleIndex);

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, tmCouponInfo, TMCouponInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("查询机构<{}>的营销活动<{}>的规则<{}>的卡券信息列表失败!", instId, mcNo, ruleIndex);
            return null;
        }

        return objectList.stream().map(item -> (TMCouponInfo) (item)).collect(Collectors.toList());
    }

    /**
     * 卡券核销数据库操作接口实现
     *
     * @param tmCouponInfo 卡券信息表
     * @return int 返回码
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/7/22 17:26
     */
    @Override
    public int redeemSingleCoupon(TMCouponInfo tmCouponInfo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "redeemSingleCoupon");

        return dbsUtil.dbsExecuteSql(sqlStr, tmCouponInfo, TMCouponInfo.class);
    }

    /**
     * 领取免费型卡券更新卡券相关信息和卡券有效期
     *
     * @param tmCouponInfo 卡券信息
     * @return int
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/8/11 19:55
     */
    @Override
    public int updateCouponInfoAndDate(TMCouponInfo tmCouponInfo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "mompUpdateCouponInfoAndDate");

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmCouponInfo, TMCouponInfo.class);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("更新法人机构号<{}>卡券编号<{}>信息失败! RETURN_CODE=<{}>", tmCouponInfo.getInstId(), tmCouponInfo.getCouponNo(), iReturnCode);
            return CommonConstant.PROCESS_FAILED;
        }
        return CommonConstant.PROCESS_SUCCESS;
    }

    /**
     * 使用营销活动规则查询一张可领取的免费卡券数据库操作接口定义
     *
     * @param tmCouponInfo 卡券信息
     * @return int
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/8/12 19:55
     */
    @Override
    public TMCouponInfo mompSelectClaimableFreeCoupon(TMCouponInfo tmCouponInfo, String couponNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);

        String sqlStr;
        if (null != couponNo && !"".equals(couponNo) && !CommonConstant.ONE_STAR.equals(couponNo) && !CommonConstant.WHITE_SPACE.equals(couponNo)) {
            //卡券编号赋值
            tmCouponInfo.setCouponNo(couponNo);
            sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "mompSelectClaimableCouponbyCouponNo");
        } else {
            sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectClaimableCouponbyRuleIndex");
        }

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmCouponInfo, tmCouponInfo);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("法人机构号<{}>获取卡券编号<{}>信息失败! RETURN_CODE=<{}>", tmCouponInfo.getInstId(), tmCouponInfo.getCouponNo(), iReturnCode);
            return null;
        }

        return tmCouponInfo;
    }

    @Override
    public TMCouponInfo selectUserCouponCountByMarketingPlatform(String mcNo, String userId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectUserCouponCountByMarketingPlatform");

        // 按接口赋查询条件
        TMCouponInfo tmCouponInfo = new TMCouponInfo();
        tmCouponInfo.setMcNo(mcNo);
        tmCouponInfo.setClaimUserId(userId);

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmCouponInfo, tmCouponInfo);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("获取卡券卡券营销活动<{}领取人<{}>可领取卡券信息失败! RETURN_CODE=<{}>", mcNo, userId, iReturnCode);
            return null;
        }

        return tmCouponInfo;
    }

    /**
     * @Description 获取【已领取】卡券信息 (统计数量)
     * @Author liujinan
     * @date 2020-09-23 17:03:57
     **/
    @Override
    public TMCouponInfo selectTMCouponClaimedCount(String instId, String mcNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectTMCouponClaimedCount");

        // 按接口赋查询条件
        TMCouponInfo tmCouponInfo = new TMCouponInfo();
        tmCouponInfo.setInstId(instId);
        tmCouponInfo.setMcNo(mcNo);

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmCouponInfo, tmCouponInfo);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("根据【机构编码】<{}> 【营销活动号】<{}>获取已领取卡券信息失败! RETURN_CODE=<{}>", instId, mcNo, iReturnCode);
            return null;
        }

        return tmCouponInfo;
    }

    /**
     * @Description 获取【已核销】卡券信息 (统计数量)
     * @Author liujinan
     * @date 2020-09-23 17:04:06
     **/
    @Override
    public TMCouponInfo selectTMCouponRedeemedCount(String instId, String mcNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectTMCouponRedeemedCount");

        // 按接口赋查询条件
        TMCouponInfo tmCouponInfo = new TMCouponInfo();
        tmCouponInfo.setInstId(instId);
        tmCouponInfo.setMcNo(mcNo);

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmCouponInfo, tmCouponInfo);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("根据【机构编码】<{}> 【营销活动号】<{}>获取已核销卡券信息失败! RETURN_CODE=<{}>", instId, mcNo, iReturnCode);
            return null;
        }

        return tmCouponInfo;
    }

    /**
     * 用户查询自己的卡券信息（分页查询）
     *
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2021/1/18 15:37
     */
    @Override
    public PageResult getUserCouponListInfoPage(TMCouponInfo tMCouponInfo, PageInfo pageInfo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "mompSelectCouponInfoByUser");

        return dbsUtil.dbsFetchPageListData(sqlStr, tMCouponInfo, TMCouponInfo.class, pageInfo);
    }

    /**
     * 查询用户在某一周期内已领取券信息 数量
     *
     * @param instId          法人机构号
     * @param mcNo            活动编号
     * @param userId          用户ID
     * @param periodStartDate 周期开始日期
     * @param periodEndDate   周期结束日期
     * @return
     * @throws Exception
     */
    @Override
    public TMCouponInfo selectUserCouponCountByInstIdAndMcNoAndDate(String instId, String mcNo, String userId, String periodStartDate, String periodEndDate) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectUserCouponCountByInstIdAndMcNoAndDate");

        // 按接口赋查询条件
        TMCouponInfo tmCouponInfo = new TMCouponInfo();
        tmCouponInfo.setInstId(instId);
        tmCouponInfo.setMcNo(mcNo);
        tmCouponInfo.setClaimUserId(userId);
        tmCouponInfo.setCouponStartDate(periodStartDate);
        tmCouponInfo.setCouponEndDate(periodEndDate);

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmCouponInfo, tmCouponInfo);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("获取卡券卡券营销活动<{}领取人<{}>可领取卡券信息失败! RETURN_CODE=<{}>", mcNo, userId, iReturnCode);
            return null;
        }

        return tmCouponInfo;
    }
}
